The Education Frontier
By Grant Kwai
Copyright (c) 1991 Apple Users' Group, Sydney
Republished from Applecations, a publication of the Apple Users' Group, Sydney, Australia.
This will be the first of a series of BASIC programs aimed at the primary and secondary school syllabus. Hopefully, programs will range from mathematics to economics with detailed analysis of how they function.
To start off this series, I will outline a topic in the 3 unit maths syllabus called Permutations and Combinations. This subject deals with the determination of things such as: in how many ways can 4 people be chosen from 23 if all have a likely chance of success? (For those of you who are wondering, this is a combination and the answer is 8855). I am sure anyone who is familiar with this topic will know what I am talking about.
Below is the short program which will work out both the permutation and combination of the users data.
Listing 1
---------
]NEW
10 HOME
20 PRINT "PERMUTATIONS AND COMBINATIONS"
30 PRINT " BY GRANT KWAI ":PRINT
40 PRINT "TYPE '0' TO END. "
50 PRINT: PRINT "ENTER TOTAL AMOUNT OF OBJECTS: ";:INPUT OBJ
60 IF OBJ = 0 THEN GOTO 1000
70 PRINT: PRINT "ENTER SIZE OF THE SUBGROUP ";: INPUT SUB
80 IF SUB < OBJ THEN PRINT "THE SUBGROUP MUST BE LESS THAN THE OBJECTS.": PRINT "PRESS ANY KEY TO CONTINUE": CALL -756 : GOTO 10
100 P = 1: C = 1
110 FOR I = OBJ - SUB + 1 TO OBJ
120 IF 9.9E35 / I > P THEN GOTO 150
130 PRINT "MORE THAN 9.9E35 PERMUTATIONS."
140 GOTO 1000
150 P = P * I
160 NEXT I
170 FOR A = 2 TO SUB
180 B = B * A
190 NEXT A
200 PRINT P ;" PERMUTATIONS"
210 PRINT : PRINT P / C;" COMBINATIONS"
220 PRINT : PRINT "PRESS ANY KEY TO CONTINUE"
230 CALL -756
240 GOTO 10
1000 PRINT: PRINT "RUN PROGRAM AGAIN?(Y/N)": GET RE$
1010 IF RE$="Y" OR RE$="y" THEN GOTO 10
1020 PRINT: PRINT" BYE!! "
1030 END
Explanation
-----------
Line
10-40 Clears screen, PRINTs message to screen.
50 Waits for user's INPUT of numbers.
60 Checks to see if input was '0' to GO TO line 1000
70 Waits for user's INPUT on size of subgroup
80 Checks to make sure subgroup size is legal. If not, loop back to line 10
100 Initialise the counters.
110 Formula for calculations
120 Makes sure we don't get an overflow error
130 PRINTs message to screen
140 GO TO line 1000
150-190 Further calculations
200-210 Displays the number of permutations and combinations.
220-230 Waits for a key press.
240 Loops back to line 10 to start again.
1000-1030 Catches overflow which would have occurred in line 110-130. Asks if we want to re-run program. Waits for a 'Y' or 'y' response to re-run, otherwise end.

THIS CONTENT COPYRIGHT © 2007, APPLE MACINTOSH USERS' GROUP, SYDNEY
Permission has been obtained to make this material available on the Internet.

Permission is hereby granted for non-profit user groups to republish this content.
PLEASE CREDIT THE AUTHOR AND THE SOURCE: Applecations, publication of the Apple Users' Group, Sydney, Australia

THIS PAGE COPYRIGHT © 2007, ANDREW ROUGHAN